﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Dal.Models;
using Framework.Common.Extention;
using Framework.Data;
using System.Web.Security;
using Dal.Entities;
namespace Web.Controllers
{
    public class LoginController : Controller
    {
        //
        // GET: /Login/
        [HttpGet]
        public ActionResult Index()
        {
            return View();
        }

        //
        // GET: /Login/Details/5
        [HttpPost]
        public ActionResult Index(User user)
        {
            if (user.UserName == "昆药集团" && user.Password == "昆药集团")
            {
                FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
                        1, // 版本
                        "昆药集团" + "|" + 0 + "|1|00000000000|0", // 用户名
                        DateTime.Now, // 票据的签发时间
                        DateTime.Now.AddHours(1), // 票据的过期时间
                        false, // 是否为永久Cookie
                        "1" // 附加的用户数据，此处用来存放用户角色
                        );

                string encryptedTicket = FormsAuthentication.Encrypt(ticket);
                HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
                Response.Cookies.Add(cookie);

                return RedirectToAction("manage", "doc");
            }
            else
            {
                var dbUser = RepositoryFactory
                    .GetRepository()
                    .First<CHACTOR>(i => i.PHONE == user.UserName && i.PHONE == user.Password);
                if (dbUser != null)
                {
                    FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
                        1, // 版本
                        dbUser.NAME + "|" + dbUser.ID + "|0|" + dbUser.PHONE + "|0", // 用户名
                        DateTime.Now, // 票据的签发时间
                        DateTime.Now.AddHours(1), // 票据的过期时间
                        false, // 是否为永久Cookie
                        "1" // 附加的用户数据，此处用来存放用户角色
                        );

                    string encryptedTicket = FormsAuthentication.Encrypt(ticket);
                    HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
                    Response.Cookies.Add(cookie);

                    return RedirectToAction("manage", "doc");
                }
                else
                {
                    ViewBag.HasError = false;
                }
            }
            return View();
        }

        [HttpGet]
        public ActionResult MsgUser()
        {
            return View();
        }

        [HttpPost]
        public ActionResult MsgUser(User user)
        {

            var dbUser = RepositoryFactory
                .GetRepository()
                .First<User>(i => i.UserName == user.UserName && i.Password == user.Password);
            if (dbUser != null)
            {
                FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
                    1, // 版本
                    dbUser.UserName + "|" + dbUser.ID + "|" + dbUser.Role + "|" + dbUser.Tel + "|1", // 用户名
                    DateTime.Now, // 票据的签发时间
                    DateTime.Now.AddHours(1), // 票据的过期时间
                    false, // 是否为永久Cookie
                    "1" // 附加的用户数据，此处用来存放用户角色
                    );

                string encryptedTicket = FormsAuthentication.Encrypt(ticket);
                HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
                Response.Cookies.Add(cookie);

                return RedirectToAction("manage", "Tongxun");
            }
            else
            {
                ViewBag.HasError = false;
            }

            return View();
        }

    }
}
